#include <iostream>
#include <cmath>

using namespace std;

// 抽象基类 Shape
class Shape {
public:
    virtual double calculateArea() const = 0; // 计算面积
    virtual bool isValid() const = 0;       // 判断形状是否合法
    virtual ~Shape() {}                   // 虚析构函数
};

// 长方形类 Rectangle
class Rectangle : public Shape {
private:
    double width;
    double height;

public:
    Rectangle(double w, double h) : width(w), height(h) {}

    double calculateArea() const override {
        return width * height;
    }

    bool isValid() const override {
        return width > 0 && height > 0;
    }
};

// 正方形类 Square
class Square : public Shape {
private:
    double side;

public:
    Square(double s) : side(s) {}

    double calculateArea() const override {
        return side * side;
    }

    bool isValid() const override {
        return side > 0;
    }
};

// 三角形类 Triangle
class Triangle : public Shape {
private:
    double a, b, c;

public:
    Triangle(double x, double y, double z) : a(x), b(y), c(z) {}

    double calculateArea() const override {
        double s = (a + b + c) / 2;
        return sqrt(s * (s - a) * (s - b) * (s - c));
    }

    bool isValid() const override {
        // 判断三角形是否合法：两边之和大于第三边
        return (a + b > c) && (a + c > b) && (b + c > a) && (a > 0) && (b > 0) && (c > 0);
    }
};

int main() {
    Rectangle rect(5.0, 3.0);
    Square square(4.0);
    Triangle tri(3.0, 4.0, 5.0);

    if (rect.isValid()) {
        cout << "长方形面积: " << rect.calculateArea() << endl;
    } else {
        cout << "长方形不合法" << endl;
    }

    if (square.isValid()) {
        cout << "正方形面积: " << square.calculateArea() << endl;
    } else {
        cout << "正方形不合法" << endl;
    }

    if (tri.isValid()) {
        cout << "三角形面积: " << tri.calculateArea() << endl;
    } else {
        cout << "三角形不合法" << endl;
    }

    return 0;
}
